package com.wobito.pojo.validator;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author Wangbaba
 * @Date 2024/3/18 17:29
 * @Version 1.0
 * 验证数据
 */
public class ValidatorUtils {

    /**
     * 手机号验证
     */
    private static final Pattern mobile_pattern = Pattern.compile("[1]([3-9])[0-9]{9}$");

    /**
     * 时间格式验证 eg：7:00
     */
    private static final Pattern time_pattern = Pattern.compile("^(0?[1-9]|1[0-2]):(0?[0-9]|[1-5]?[0-9])$");

    /**
     * 文件名称验证 只允许数字、字母、下划线、中划线
     * @param mobile
     * @return
     */
    private static final Pattern filename_pattern = Pattern.compile( "^(?!.*?\\.$)[a-zA-Z0-9_-]+(?!^\\.)(?:\\.[a-zA-Z]{2,6})?$");

    //手机验证
    public static boolean isMobile(String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return false;
        }
        Matcher matcher = mobile_pattern.matcher(mobile);
        return matcher.matches();
    }

    //时间验证
    public static boolean isTime(String time) {
        if (StringUtils.isEmpty(time)) {
            return false;
        }
        Matcher matcher = time_pattern.matcher(time);
        return matcher.matches();
    }

    //数字验证
    public static boolean isNumber(String number) {
        if (StringUtils.isEmpty(number)) {
            return false;
        }
        for (int index = 0; index < number.length(); index++) {
            if (!Character.isDigit(number.charAt(index))) {
                return false;
            }
        }
        return true;
    }

    //文件名称验证
    public static boolean isFileName(String fileName) {
        if (fileName.length() < 1||fileName.length() > 255) {
            return false;
        }
       Matcher matcher = filename_pattern.matcher(fileName);
        return matcher.matches();
    }
}
